package com.opencloud.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.opencloud.common.model.PageParams;
import com.opencloud.common.model.Result;
import com.opencloud.common.security.http.XRestTemplate;
import com.opencloud.system.client.model.AuthorityAction;
import com.opencloud.system.client.model.entity.SystemAction;
import com.opencloud.system.client.service.ISystemActionServiceClient;
import com.opencloud.system.service.SystemActionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author liuyadu
 */
@Api(tags = "系统功能按钮")
@RestController
public class SystemActionController implements ISystemActionServiceClient {
    @Autowired
    private SystemActionService systemActionService;
    @Autowired
    private XRestTemplate xRestTemplate;

    @ApiOperation(value = "获取功能操作列表", notes = "获取功能操作列表")
    @GetMapping("/action")
    @Override
    public Result<Page<AuthorityAction>> getPage(@RequestParam Map<String, Object> map) {
        return Result.ok().data(systemActionService.findPage(new PageParams(map)));
    }

    @ApiOperation(value = "获取功能按钮详情", notes = "获取功能按钮详情")
    @GetMapping("/action/info")
    @Override
    public Result<AuthorityAction> get(@RequestParam("actionId") Long actionId) {
        return Result.ok().data(systemActionService.getById(actionId));
    }

    @ApiOperation(value = "添加/修改功能按钮", notes = "添加/修改功能按钮")
    @PostMapping("/action/save")
    @Override
    public Result<Long> save(@RequestBody SystemAction systemAction) {
        if (systemAction.getActionId() == null) {
            systemActionService.add(systemAction);
        } else {
            systemActionService.update(systemAction);
        }
        xRestTemplate.refreshGateway();
        return Result.ok();
    }

    @ApiOperation(value = "移除功能按钮", notes = "移除功能按钮")
    @PostMapping("/action/remove")
    @Override
    public Result remove(@RequestParam("actionId") Long actionId) {
        systemActionService.remove(actionId);
        // 刷新网关
        xRestTemplate.refreshGateway();
        return Result.ok();
    }
}
